// JavaScript Document

function showLabelDialogAdd() {
	resetLabelDialog();
	$("#label-dialog > h2").text("Adding...");
	$("#labelAddOption").css("display","block");
	$("#labelEditOption").css("display","none");
	$("#mask").css("display","block");
	$("#label-dialog").css("display","block");
}

function shutLabelDialog() {
	$("#mask").css("display","none");
	$("#label-dialog").css("display","none");
}
function showLabelDialogEdit(obj) {
	resetLabelDialog();
	$("#label-dialog > h2").text("Editing...");
	$("#_label_name").val(obj.innerHTML);
	$("#_label_id").val($(obj).attr("keyString"));
	$("#labelAddOption").css("display","none");
	$("#labelEditOption").css("display","block");
	$("#mask").css("display","block");
	$("#label-dialog").css("display","block");
	
}
function addLabel() {
	var name = jQuery.trim($("#_label_name").val());
	if(name ==null || name == "") {
		return;
	}else {
		$.ajax({
			type:"post",
			url:"label.htm",
			data:"method=add&name="+name,
			success:function(msg){
				var id = msg.split(":")[0];
				if(id == "-1") {
					$("#label-dialog > .tip").html("<span class='red'>"+msg.split(":")[1]+"</span>");
				}else {
					//alert("Data Saved:"+msg);
					var newLabel = "<tr keyString='"+id+"' class='new'><td><input type='checkbox' keyString='"+id+"'/></td>"
						+"<td><a href='javascript:void()' onclick='showLabelDialogEdit(this);return false;' keyString='"
						+id+"'>"
						+name+"</a></td>"
						+"<td></td></tr>";
					$("#ct-table-optionbar").after(newLabel);
					shutLabelDialog();
				}
			}
		});
	}
}
function editLabel() {
	var name = jQuery.trim($("#_label_name").val());
	var keyString = $("#_label_id").val();
	if(name ==null || name == "") {
		return;
	}else {
		$.ajax({
			type:"post",
			url:"label.htm",
			data:"method=edit&name="+name+"&keyString="+keyString,
			success:function(msg){
				if(msg == null || msg =="") {
					$("#mask").css("display","none");
					$("#label-dialog").css("display","none");
					$("a[keyString='"+keyString+"']").text(name);
				}else {
					$("#label-dialog > .tip").html("<span class='red'>"+msg+"</span>");
				}
			}
		});
	}
}
function addLabelAndNext() {
	var name = jQuery.trim($("#_label_name").val());
	if(name ==null || name == "") {
		return;
	}else {
		$.ajax({
			type:"post",
			url:"label.htm",
			data:"method=add&name="+name,
			success:function(msg){
				var id = msg.split(":")[0];
				if(id == "-1") {
					$("#label-dialog > .tip").html("<span class='red'>"+msg.split(":")[1]+"</span>");
				}else {
					var newLabel = "<tr keyString='"+msg+"' class='new'><td><input type='checkbox' keyString='"+msg+"'/></td>"
						+"<td><a href='javascript:void()' onclick='showLabelDialogEdit(this);return false;' keyString='"
						+msg+"'>"
						+name+"</a></td>"
						+"<td></td></tr>";
					$("#ct-table-optionbar").after(newLabel);
					resetLabelDialog();
				}
			}
		});
	}
}

function del() {
	var delIdArray = new Array();
	$("input[type='checkbox']").each(function(i){
		if(this.checked) {
			delIdArray.push($(this).attr('keyString'));
			}
	});
	var delIdString = delIdArray.toString();
	if(delIdString == null ||delIdString == "") return;
	else {
		$.ajax({
			type:"POST",
			url:"label.htm",
			data:"method=del&delIdString="+delIdString,
			success:function(msg){
				for(var idArray in delIdArray) {
					$("tr[keyString='"+delIdArray[idArray]+"']").remove();
				}
			}
		});
	}
	//alert(delIdArray.toString());
}

function resetLabelDialog() {
	$("#_label_name").val("");
	$("#label-dialog > .tip").html("");
}

function selectAll() {
	$("input[type='checkbox']").attr("checked", true);
}
function unSelectAll() {
	$("input[type='checkbox']").attr("checked", false);
}